#!/bin/bash
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 11 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#


## p_agent_package_redhat_repomod <path> <id> <entry>
##
## Modifies a Yum repository file to adjust its configuration.
##
## <path>     Absolute path to the .repo file
## <id>       Short name of the repository
## <entry>    Entry line from the P_AGENT_PACKAGE_REPOLIST
##
## Returns 0 on success, non-zero on failure.
##
function p_agent_package_redhat_repomod() {
	local status=0
	local item=
	local remainder="$3"
	local name=
	local value=
	local temp=
	
	temp=$(s_mktempfile)
	status=$?
	
	if [[ ${status} -eq 0 ]]; then
		echo "defvar $2 /files$1" > "${temp}"
	
		item=$(echo "${remainder}" | sed 's/|.*//')
		remainder=$(echo "${remainder}" | sed 's/^.*|//')
		while [[ -n "${item}" ]]; do
			name=$(echo "${item}" | sed 's/=.*//')
			value=$(echo "${item}" | sed 's/.*=//')
		
			if [[ "${name}" != "id" ]]; then
				echo "set $2/${name} ${value}" >> "${temp}"
			fi
		
			item=$(echo "${remainder}" | sed 's/|.*//')
			remainder=$(echo "${remainder}" | sed 's/^.*|//')
		done
		
		augtool -sf "${temp}"
		status=$?
	fi
	return ${status}
}


## p_agent_package_redhat_repos
##
## For each entry in the P_AGENT_PACKAGE_REPOLIST array, looks for the
## .repo file defining the repository with the matching id. Modifies the
## repository file according to the entry. Returns 0 on success, non-zero
## if an error occurs. Continues processing on error, returns the exit
## status of the first error.
##
function p_agent_package_redhat_repos() {
	local pathbase="${P_ROOT}/etc/yum.repos.d"
	local status=0
	local name=
	
	for entry in "${P_AGENT_PACKAGE_REPOLIST[@]}"; do
		name=$(extract_element "id" "${entry}")
		if [[ -n "${name}" ]]; then
			for fname in $(ls "${pathbase}/*.repo"); do
				cat "${pathbase}/${fname}" | grep "^\[${name}\]$" > /dev/null 2&1
				if [[ $? -eq 0 ]]; then
					p_agent_package_redhat_repomod "${pathbase}/${fname}" \
						"${name}" "${entry}"
					status=$(merge_status ${status} $?)
				fi
			done
		fi
	done
	
	return ${status}
}


## p_agent_package_redhat_install
##
## Installs any additional packages requested in the subscription using
## yum(8). Returns 0 on success, non-zero if an error occurs.
##
function p_agent_package_redhat_install() {
	local name=
	local from=
	local status=0
	local yumopts=
	local count=0
	local -a bulkgroup
	
	if [[ -n "${P_ROOT}" ]]; then
		yumopts="--installroot=${P_ROOT}"
	fi
	
	# Iterate through the list of packages that have installation requests.
	# Install packages from specific repositories as we go, but group all
	# the packages without repository specifiers into the bulkgroup for
	# efficiency.
	for entry in "${P_AGENT_PACKAGE_PKGLIST[@]}"; do
		name=$(extract_element "name" "${entry}")
		from=$(extract_element "from" "${entry}")
		
		if [[ -n "${from}" ]]; then
			yum ${yumopts} -y --disablerepo='*' --enablerepo="${from}" \
				install "${name}"
			#
			status=$(merge_status ${status} $?)
		else
			bulkgroup+=("${name}")
			((count++))
		fi
	done
	
	if [[ ${count} -gt 0 ]]; then
		yum ${yumopts} -y install "${bulkgroup[@]}"
		status=$(merge_status ${status} $?)
	fi
	
	return ${status}
}


## p_agent_package_redhat_check
##
## Implements update-checking logic for the Package agent on the Red Hat
## platform. Calls yum check-update to determine if updates are available.
## Returns 0 if no updates are available, 100 if updates are available, or
## another non-zero value if an error occurs.
##
function p_agent_package_redhat_check() {
	local yumopts=
	
	if [[ -n "${P_ROOT}" ]]; then
		yumopts="--installroot=${P_ROOT}"
	fi
	
	yum ${yumopts} check-update
	
	return $?
}


## p_agent_package_redhat_run
##
## Updates a Red Hat system and installs any extra requested packages.
## Returns 0 on success, non-zero if an error occurs.
##
function p_agent_package_redhat_run() {
	local yumopts=
	local result=
	local status=0
	
	if [[ -n "${P_ROOT}" ]]; then
		yumopts="--installroot=${P_ROOT}"
	fi
	
	p_agent_package_redhat_repos
	status=$?
	
	echo "Updating installed packages..."
	yum ${yumopts} -y update
	status=$(merge_status ${status} $?)
	
	echo "Installing additional packages..."
	p_agent_package_redhat_install
	status=$(merge_status ${status} $?)
	
	return ${status}
}
